package com.ibm.cep.test.adaptor.vehicledata

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.sql.Timestamp
import java.util.zip.ZipOutputStream;

import com.ibm.cep.util.ZipUtil;

class VehicleDataZipGeneration4CI {

	private static final String MONTH = "JUL";

	private static final def CEP_VEHICLE_DATA_DIR = "C:\\temp\\CEP\\vehicle_data\\CI\\Jul\\";

	private static final def CEP_VEHICLE_DATA_ZIP_DIR = "C:\\temp\\CEP\\vehicle_data\\CI\\${MONTH}_archive_part6\\";
//	private static final def CEP_VEHICLE_DATA_ZIP_DIR = "H:\\wanghe\\IBM\\Smart in Car\\CEP\\vehicle_data\\CI\\${MONTH}_archive_part4\\";

	static void main(args){
		def srcFolder = new File(CEP_VEHICLE_DATA_DIR)
		def zipFolder = new File(CEP_VEHICLE_DATA_ZIP_DIR)
		if(!zipFolder.exists()){
			zipFolder.mkdirs()
		}

		def fileIndex = 0;
		def lineIndex = 0;
		def zipIndex = 150;//150, 240
		def map = new HashMap();
		def lastName = null;
		def files = srcFolder.listFiles();
		Arrays.sort(files, new Comparator<File>(){
					int compare( arg0,  arg1) {
						return Integer.valueOf(arg0.name.replace("data", "").replace(".txt", "")).compareTo(Integer.valueOf(arg1.name.replace("data", "").replace(".txt", "")));
					};
				})
		def needContinue = true;
		ZipOutputStream zos = null;
		files.each {

			if(fileIndex<= 3001){
				println it.name
				if(fileIndex >= 10 && fileIndex % 100 == 0){
					if(zos != null){
						zos.close();
					}
					zos = null;
				}
				if(zos == null){
					def result  = new File(zipFolder, "RawData_CI_" + MONTH + "_" + String.format('%1$03d', (zipIndex + 1)) + ".zip");
					zos = new ZipOutputStream(
							new BufferedOutputStream(new FileOutputStream(result, true),
							1024 * 100));
					zipIndex ++;
				}
				map.put(it.name, it.readBytes())
				lastName = it.name;
				fileIndex ++;
				// sum total line count
				it.eachLine{ lineIndex++; }
				//						lineIndex --;

				if(map.size() >= 5){
					ZipUtil.addFileToZip(zos, map)
					map.clear();
				}
			}
		}
		if(map.size() > 0){
			ZipUtil.addFileToZip(zos, map)
			map.clear();
		}
		if(zos != null){
			zos.close();
		}
		println "total file count: " + fileIndex;
		println "total line count: " + lineIndex;
	}

}
